<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Request;
use think\facade\Session;

class Index extends Controller
{
    //记录用户登陆数据，防止重复登陆
    public function initialize(){
        if (Session::has('admin')){
            $this->redirect('admin/home/index');
        }
    }

    //后台管理员登录
    public function login(Request $request){
        if ($request->isAjax()){
            $data = ['username' => $request->post('username'),
                     'password' => $request->post('password'),
                     'ip'       => $request->ip()];
            $model = new Admin;
            $result = $model->login($data);
            if ($result==1){
                $data=['code'=>1,'msg'=>'登录成功','url'=>'index'];
                return json($data);
            }else{
                $data=['code'=>0,'msg'=>$result];
                return json($data);
            }
        }
        return view();
    }

    //后台管理员注册
    public function register(Request $request){
        if ($request->isAjax()){
            $data = [
                'username' => $request->post('username'),
                'password' => $request->post('password'),
                'confirm' => $request->post('confirm'),
                'nickname' => $request->post('nickname'),
                'email' => $request->post('email')
            ];
            $model = new Admin;
            $result = $model->register($data);
            if ($result==1){
                mailto($data['email'],'后台管理员账户注册成功','您刚刚注册的账户' . $data['username']  . '已创建成功！！');
                $data = ['code'=>1,'msg'=>'用户注册成功','url'=>'login'];
                return json($data);
            }else{
                $data = ['code'=>0,'msg'=>$result];
                return json($data);
            }
        }
        return $this->fetch();
    }

    //用户忘记密码
    public function forget(Request $request){
        if ($request->isAjax()) {
            $data = [
                'email' => $request->post('email')
            ];
            $model = new Admin;
            $result = $model->forget($data);
            if ($result == 1) {
                $code = mt_rand(1000, 9999);
                mailto($data['email'], '您的重置密码验证码', '您的重置密码验证码为' . $code);
                Session::set('forget', ['email' => $data['email'], 'code' => $code]);
                return json(['code'=>1]);
            } else {
                return json(['code'=>0,'msg'=>$result]);
            }
        }
        return $this->fetch();
    }

    //用户忘记密码后重置密码
    public function reset(Request $request){
        if ($request->isAjax()){
            $data =  [
                'code' => Session::get('forget')['code'],
                'email'=> Session::get('forget')['email'],
                'codein'=>$request->post('codein')
            ];
            $model = new Admin;
            $result = $model->reset($data);
            if ($result==1){
                return json(['code'=>1,'msg'=>"重置密码成功，请到邮箱中查找新密码",'url'=>'login']);
            }else{
                return json(['code'=>0,'msg'=>$result]);
            }
        }
        return $this->fetch();
    }


}
